﻿using BLL;
using BLL.Dal;
using BLL.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SysAdmin_User_List : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["paramType"] != null && Request["paramType"] != "")
        {
            switch (Request["paramType"])
            {
                case "QueryList": QueryList(); break;
                case "AddModel": AddModel(); break;
                case "DelModel": DelModel(); break;
                case "QueryModel": QueryModel(); break;
                case "UpdateModel": UpdateModel(); break;
                default: break;
            }
        }
    }

    /// <summary>
    /// 获取所有的分页展示数据
    /// </summary>
    void QueryList()
    {

        //string strWhere = "";

        int PageCount = 0;

        if (Request["page"] != null)
        {
            int.TryParse(Request["page"], out PageCount);
        }

        int RowsCount = 30;

        if (Request["rows"] != null)
        {
            int.TryParse(Request["rows"], out RowsCount);
        }

        SysUserDAL userBus = new SysUserDAL();
        int recordCount = userBus.GetRecordCount("");

        DataTable dtData = userBus.GetListByPage("", "", (RowsCount * (PageCount - 1)), RowsCount * PageCount).Tables[0];

        SysDictionaryDAL dicBus = new SysDictionaryDAL();
        Dictionary<string, string> dicSex = dicBus.GetDicFromParentCode("sex");

        //角色和生日展示

        RoleDAL roleBus = new RoleDAL();
        for (int i = 0; i < dtData.Rows.Count; i++)
        {
            Role model = roleBus.GetModel(dtData.Rows[i]["roleCode"].ToString());
            if (model != null)
            {
                dtData.Rows[i]["roleCode"] = model.roleName;
            }
            if (dtData.Rows[i]["birthday"] != null && dtData.Rows[i]["birthday"].ToString() != "")
            {
                dtData.Rows[i]["temp_text"] = Convert.ToDateTime(dtData.Rows[i]["birthday"]).ToString("yyyy-MM-dd");
            }
            else
            {
                dtData.Rows[i]["temp_text"] = "";
            }
            //性别处理
            if (dtData.Rows[i]["sex"] != null && dtData.Rows[i]["sex"].ToString() != "")
            {
                if (dicSex.ContainsKey(dtData.Rows[i]["sex"].ToString()))
                {
                    dtData.Rows[i]["sex"] = dicSex[dtData.Rows[i]["sex"].ToString()].ToString();
                }
            }
            else
            {
                dtData.Rows[i]["sex"] = "";
            }
        }


        object obj = new { total = recordCount, rows = dtData };

        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);

        WebHelper.HttpResponseJson(response);
    }


    /// <summary>
    /// 增加新的项
    /// </summary>
    void AddModel()
    {
        SysUserDAL modelBus = new SysUserDAL();
        SysUser model = new SysUser();
        model.ID = Guid.NewGuid();
        model.userName = ReqHelper.Get<string>("user_name");
        model.userPwd = EncryptHelper.ComputeMD5Value(ReqHelper.Get<string>("user_pwd"));
        model.fullName = ReqHelper.Get<string>("user_full_name");
        model.sex = ReqHelper.Get<string>("user_sex");
        model.birthday = null;
        if (Request["user_add_birthday"] != null && Request["user_add_birthday"] != "")
        {
            model.birthday = Convert.ToDateTime(Request["user_add_birthday"]);
        }
        model.roleCode = ReqHelper.Get<string>("user_add_rolecombobox");

        object obj = new { success = true, msg = "数据操作成功" };
        try
        {
            if (modelBus.Add(model))
            {
                //添加用户权限表
                string strPowers = ReqHelper.Get<string>("user_add_powercombox");
                UserPowerDAL userPowerBus = new UserPowerDAL();
                if (strPowers != "")
                {
                    string[] strPower = strPowers.Split(',');
                    foreach (string tempPower in strPower)
                    {
                        UserPower tempUserPower = new UserPower();
                        tempUserPower.ID = Guid.NewGuid();
                        tempUserPower.userCode = model.userName;
                        tempUserPower.powerCode = tempPower;
                        userPowerBus.Add(tempUserPower);
                    }
                }

            }
            obj = new { success = true, msg = "数据操作成功" };
        }
        catch (Exception error)
        {
            obj = new { success = false, msg = error.Message };
        }
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);
    }


    /// <summary>
    /// 删除选择的项
    /// </summary>
    void DelModel()
    {
        string strCode = ReqHelper.Get<string>("userCode");
        SysUserDAL userBus = new SysUserDAL();
        object obj = new { success = userBus.Delete(new Guid(strCode)) };
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);
    }

    /// <summary>
    /// 查询显示项
    /// </summary>
    void QueryModel()
    {

    }

    /// <summary>
    /// 更新选择的项
    /// </summary>
    void UpdateModel()
    {
        SysUserDAL userBus = new SysUserDAL();
        SysUser model = userBus.GetModel(new Guid(Request["user_edit_key"]));
        if (ReqHelper.Get<string>("user_pwd") != "")
        {
            model.userPwd = EncryptHelper.ComputeMD5Value(ReqHelper.Get<string>("user_pwd"));
        }
        model.fullName = ReqHelper.Get<string>("user_full_name");
        model.sex = ReqHelper.Get<string>("user_sex");
        model.birthday = null;
        if (Request["user_edit_birthday"] != null && Request["user_edit_birthday"] != "")
        {
            model.birthday = Convert.ToDateTime(Request["user_edit_birthday"]);
        }
        model.roleCode = ReqHelper.Get<string>("user_edit_rolecombobox");

        object obj = new { success = true, msg = "数据操作成功" };
        try
        {
            if (userBus.Update(model))
            {
                //添加用户权限表
                string strPowers = ReqHelper.Get<string>("user_edit_powercombotree");
                UserPowerDAL userPowerBus = new UserPowerDAL();
                userPowerBus.Delete(model.userName);
                if (strPowers != "")
                {
                    string[] strPower = strPowers.Split(',');
                    foreach (string tempPower in strPower)
                    {
                        UserPower tempUserPower = new UserPower();
                        tempUserPower.ID = Guid.NewGuid();
                        tempUserPower.userCode = model.userName;
                        tempUserPower.powerCode = tempPower;
                        userPowerBus.Add(tempUserPower);
                    }
                }

            }
            obj = new { success = true, msg = "数据操作成功" };
        }
        catch (Exception error)
        {
            obj = new { success = false, msg = error.Message };
        }
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);



    }
}